- Java PDF Generation with IText
- Getting Started with IText PDF API for Java
- Java IText: Document
- Java IText: Chunk
- Java IText: Phrase
- Java IText: Paragraph
- Java IText: Chapter + Section
- Java IText: Anchor (link)
- Java IText: List
- Java IText: Table
- Java IText: Image
- Java IText: Superscript + Subscript
- Java IText: Underline + Strikethrough
- Java IText: Font
- Java IText: Modifying Existing PDF Documents
Java IText: Phrase
Jakob Jenkov |
The com.itextpdf.text.Phrase class in IText represents a "phrase" of text.
The Phrase class knows how to add spacing between lines.
Here is a simple code example:
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
public class DocumentExample {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("Phrase.pdf"));
document.open();
document.add(new Phrase("This is sentence 1. "));
document.add(new Phrase("This is sentence 2. "));
document.add(new Phrase("This is sentence 3. "));
document.add(new Phrase("This is sentence 4. "));
document.add(new Phrase("This is sentence 5. "));
document.add(new Phrase("This is sentence 6. "));
document.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Here is what the generated document looks like:
![]() |
| An IText Phrase example |
Notice how sentence 6 is nicely located on its own line.
Line Spacing
Phrase objects knows how to add line spacing if the added phrase exceeds the right edge of the document.
It does not, however, add extra space between paragraphs. For this, you need to use a Paragraph
object.
Line spacing is measured in user units. There are 72 units per inch.
The default spacing is 1.5 times the font height. You can change the line spacing by passing spacing
as a parameter to the Phrase constructor, like this:
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
public class DocumentExample {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("Phrase2.pdf"));
document.open();
Chunk chunk = new Chunk("This is a sentence ");
Phrase phrase = new Phrase(50);
phrase.add(chunk);
phrase.add(chunk);
phrase.add(chunk);
phrase.add(chunk);
phrase.add(chunk);
phrase.add(chunk);
document.add(phrase);
document.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Below is a screenshot of what the generated document looks like. Notice the extra line spacing between the two lines.
![]() |
| An IText Phrase example with custom line spacing |
| Tweet | |
Jakob Jenkov | |











